package com.kbcss.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AddUser
 */
public class AddUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
    public AddUser() {
        super();
        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("Hi....");
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		RequestDispatcher rd;
		
		String fname=request.getParameter("fname");
		String lname=request.getParameter("lname");
		String dob=request.getParameter("dob");
		String gender=request.getParameter("gender");
		String department=request.getParameter("department");
		String phno=request.getParameter("phno");
		String email=request.getParameter("email");
		String regdno=request.getParameter("regdno");
		String role=request.getParameter("role");
		String userid=fname+regdno;
		
		String str1="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		StringBuilder sb=new StringBuilder(8);
		Random r = new Random();
		System.out.println("\n\t Unique codes are \n\n");
		for(int j=0;j<8;j++)
        {
            sb.append(str1.charAt(r.nextInt(str1.length())));
        }
        System.out.println("  "+sb.toString());
        String password=sb.toString();
		//String password=request.getParameter("password");
		
		int xx=0;
		try{
		     Class.forName("com.mysql.jdbc.Driver");
		     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kbcssdb", "root", "mysql");
		     PreparedStatement ps=con.prepareStatement("insert into userdetails(userid,password,fname,lname,email,dob,gender,regdno,deptid,phno,role) values(?,?,?,?,?,?,?,?,?,?,?)");
		     ps.setString(1,userid);
		     ps.setString(2,password);
		     ps.setString(3,fname);
		     ps.setString(4,lname);
		     ps.setString(5,email);
		     ps.setString(6,dob);
		     ps.setString(7,gender);
		     ps.setString(8,regdno);
		     ps.setString(9,department);
		     ps.setString(10,phno);
		     ps.setString(11,role);
		     xx=ps.executeUpdate();
		}
		catch(Exception ex){
			out.println("error...."+ex.getMessage());
		}
		if(xx==1){
			EmailSend emailid=new EmailSend();
			System.out.println("USERNAME AND PASSWORD NOT YET SENT....");
			emailid.send(userid,password,email);
			System.out.println("USERNAME AND PASSWORD SENT....");
			
			rd=request.getRequestDispatcher("success.html");
			rd.forward(request,response);
		}
		     
		
	}

}
